import axios, { type AxiosResponse } from "axios";
// import pinia from "@/store/index";
import { showSuccessToast, showFailToast } from "vant";

/* 定义response对象的data接口 */
interface ResponseData<T> {
code: number;
data: T;
message: string;
}

// 配置新建一个 axios 实例
const request = axios.create({
baseURL: import.meta.env.VITE_BASE_URL,
timeout: 30000,
});

// 添加请求拦截器
request.interceptors.request.use((config) => {
return config;
});

// 添加响应拦截器
request.interceptors.response.use(
/* 约束一下response */
 async (response: AxiosResponse<ResponseData<any>>) => {
  const res = response.data;
  // if (res.data.ret !== 20000 && res.data.ret !== 200) {
  //  showFailToast("请求失败");
  // }
  return res.data;
 },
 (error) => {
  // 对响应错误做点什么
  if (error.message.indexOf("timeout") != -1) {
   showFailToast("网络连接超时");
  } else if (error.message == "Network Error") {
 showFailToast("网络连接错误");
 }
 return Promise.reject(error);
 }
);

export default request;